package com.wootion.cims.module.systemsetting.log.controller;

import com.wootion.cims.module.systemsetting.log.service.SysLogService;
import com.wootion.cims.response.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Optional;

/**
 * 用户管理
 * @author YaoYiChao
 */
@RestController
@RequestMapping("/syslog")
@Api(value = "日志管理")
@RequiresAuthentication
public class SysLogController {

    @Resource
    SysLogService sysLogService;

    @PostMapping(path = "/list")
    @ApiOperation(value = "用户管理-列表查询", httpMethod = "POST", notes = "{\"page\":0,\"limit\":10,\"condititon\":\"关键字\"}")
    public ResponseResult list(@RequestParam(required = false) String condition,
                               @RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "18") Integer limit,
                               @RequestParam(required = false) String begintime,
                               @RequestParam(required = false) String endtime,
                               @RequestParam(required = false) String value,
                               @RequestParam(required = false) String incident,
                               @RequestParam(required = false) String type,
                               @RequestParam(required = false) String result) {
        Date begintime1 = null;
        Date endtime1 = null;

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (Optional.ofNullable(begintime).isPresent()){
                begintime += " 00:00:00";
                begintime1 = simpleDateFormat.parse(begintime);
            }
           if (Optional.ofNullable(endtime).isPresent()){
               endtime += " 23:59:59";
               endtime1 = simpleDateFormat.parse(endtime);
           }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sysLogService.selectPage(limit,page,condition,begintime1,endtime1,value,incident,type,result);
    }
}
